'use strict';

const conn = require('../../libs/DB/Conn');
const _ = require('underscore');
const utils = require('./userGroupFunction');

module.exports = async function (params) {
    let sysMenuTable = '';
    let routerTable = '';
    let groupId = 0;
    let sysMenuType = params.sysMenuType;

    let updateSql = 'UPDATE user_group SET  ? WHERE group_id = ?',
        getSysMenuSql = `SELECT sm.* ,
                        r.path,
                        r.is_first,
                        r.redirect,
                        r.url 
                        FROM sys_menu sm 
                        LEFT JOIN 
                        router r 
                        ON r.menu_id=sm.menu_id WHERE sm.menu_type = ?;`;
    let allMenuArr = await conn(getSysMenuSql, sysMenuType);
    let userGroupAuth = utils.getUserGroupAuth(allMenuArr);//查询出权限下所有菜单后 进行权限计算 获取出该用户所在用户组中的菜单 以及路由权限
    await conn(updateSql, [{
        menus: '',
        menu: userGroupAuth.menuList,
        routes: userGroupAuth.routeList
    }, sysMenuType]);
    return { success: true };
};
